<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Notice;
use App\Models\Worklog;
use App\Models\User;
use App\Models\Rent;
use App\Services\Wechat;


class IndexController extends Controller
{
    public function index()
    {
    	$str = '/files/20220825/oa/20220825181658-1L12.png&/files/20220825/oa/20220825181658-XS15.png&/files/20220825/oa/20220825181658-SI1.png&/files/20220825/oa/20220825181658-tX7.png&/files/20220825/oa/20220825181658-n413.jpg';
        print_r(strstr('&', $str));
        return view('hrs.index');
    }

    
    public function schedule()
    {
        $users = User::where('role_code','1')->get();
        $date = \Carbon\Carbon::today()->addDays(33);
        
        // 审车到期提醒
        $list = Rent::whereDate('shenche_date', '<=', date($date))->where('status',1)->get();
        if($list) {
           foreach ($list as $k => $v) {    
                foreach ($users as $key => $user) {
                    if(empty($user->openid)) continue;
                    $msg['openid'] = $user->openid;            
                    $msg['first']  = '到期审车提醒'; 
                    $msg['car_num']      = $v->car_num;
                    $msg['shenche_date'] = $v->shenche_date;                
                    $msg['remark'] = '点击查看详情。';
                    $msg['url']    = config('app.url').'/rent-info?id='.$v->id;
                    Wechat::sendMsgTpl('shenche_end',$msg);
                }           
                
           }
        }

        
        // 商业险到期提醒
        $list = Rent::whereDate('shangyexian_date', '<=', date($date))->where('status',1)->get();
        if($list) {
           foreach ($list as $k => $v) {
                foreach ($users as $key => $user) {
                    if(empty($user->openid)) continue;               
                    $msg['openid'] = $user->openid;            
                    $msg['first']  = '商业险到期提醒'; 
                    $msg['car_num']      = $v->car_num;
                    $msg['date']   = $v->shangyexian_date;                
                    $msg['remark'] = '点击查看详情。';
                    $msg['url']    = config('app.url').'/rent-info?id='.$v->id;
                    Wechat::sendMsgTpl('baoxian_end',$msg);
                }
           }
        }

        // 交强险到期提醒
        $list = Rent::whereDate('jiaoqiangxian_date', '<=', date($date))->where('status',1)->get();
        if($list) {
           foreach ($list as $k => $v) {
                foreach ($users as $key => $user) {
                    if(empty($user->openid)) continue;                 
                    $msg['openid'] = $user->openid;            
                    $msg['first']  = '交强险到期提醒'; 
                    $msg['car_num']      = $v->car_num;
                    $msg['date']   = $v->jiaoqiangxian_date;                
                    $msg['remark'] = '点击查看详情。';
                    $msg['url']    = config('app.url').'/rent-info?id='.$v->id;
                    Wechat::sendMsgTpl('baoxian_end',$msg);
                }
           }
        }

        // 合同到期提醒
        $list = Rent::whereDate('rent_end', '<=', date($date))->where('status',1)->get();
        if($list) {
           foreach ($list as $k => $v) {
                foreach ($users as $key => $user) {
                    if(empty($user->openid)) continue;                 
                    $msg['openid'] = $user->openid;            
                    $msg['first']  = '租赁合同到期提醒'; 
                    $msg['car_num']    = $v->car_num;
                    $msg['rent_end']   = $v->rent_end;                
                    $msg['remark'] = '点击查看详情。';
                    $msg['url']    = config('app.url').'/rent-info?id='.$v->id;
                    Wechat::sendMsgTpl('rent_end',$msg);
                }
           }
        }


        $date = \Carbon\Carbon::today()->addDays(15);
        // 下次收款到期提醒
        $list = Rent::whereDate('next_pay_date', '<=', date($date))->where('status',1)->get();
        if($list) {
           foreach ($list as $k => $v) {
                foreach ($users as $key => $user) {
                    if(empty($user->openid)) continue;                 
                    $msg['openid'] = $user->openid;            
                    $msg['first']  = '收款期提醒'; 
                    $msg['money']           = $v->next_pay_price;
                    $msg['next_pay_date']   = $v->next_pay_date;                
                    $msg['remark'] = '点击查看详情。';
                    $msg['url']    = config('app.url').'/rent-info?id='.$v->id;
                    Wechat::sendMsgTpl('receive_money',$msg);
                }
           }
        }


    }

    public function rentInfo(Request $request)
    {
        $id   = $request->id;
        $data = Rent::find($id);
        return view('rent-info',compact('data'));
    }


    
}
